package ge.scenes;


import java.util.Collection;
import java.util.Collections;
import java.util.ListIterator;

import ge.modules.events.EventTrackGenerator;

/**
 * 
 * @author Abel
 *
 * The event will be activate between two clue Frames
 * 
 */
public class EventTrack extends Track 
{

	private boolean event;

	/**
	 * 
	 * @param id Identify for the Event Track. Knowing it you'll
	 * 	can listen its events with the EventTrackListener
	 */
	public EventTrack(int id)
	{
		super();
		//Doesn't need to be saved in a variable. It'll automatically
		//be attached at the Event Engine
		new EventTrackGenerator(id,this); 
	}
	
	/**
	 * 	Adds a new clue frame.
	 * 	Two frames 
	 * @param time The time of the frame
	 * @param activateFlag If the frame has to activate
	 * or not the frame.  
	 */
	public void addFrame(double time, boolean activateFlag)
	{
		clueFrames.add(new EventFrame(time, activateFlag));
		//Inefficient, but.. it's the only form to go through it
		Collections.sort(clueFrames);
	}
	
	/**
	 * The state depends of that frame
	 */
	@Override
	protected void reachFrame(Frame f)
	{
		EventFrame evf = (EventFrame) f;
		event = evf.getActivateEvent();
	}
	/**
	 * 	The state depends of the previous frame
	 */
	@Override
	protected void reachReverseFrame(Frame f) 
	{
	
	 	if(actualFrame > 0)
	 		event = ((EventFrame)clueFrames.get(actualFrame-1)).getActivateEvent();
	 	else
	 		event = false;
	}
	@Override
	protected void currentTimeJump(double time, boolean advance)
	{
	 
		actualFrame = getIndexOfFrameBefore(time);
		//Si avanzo me interesa el anterior
		if(advance)
		{
			if(actualFrame > -1 )
			{
				if (actualFrame < clueFrames.size())
					reachFrame(clueFrames.get(actualFrame));
				else //Caso especial final, depende del anterior
					reachFrame(clueFrames.get(actualFrame-1));
			}
			else 
				event = false;
		}
		else
		{
			//Si vamos aun no hemos llegado al Frame, estamos en el siguiente
			if(actualFrame > -1)
			{
				actualFrame++;	
				reachFrame(clueFrames.get(actualFrame-1));
			}
			else
				event = false;
		}
	}
	/**
	 * 	
	 * @return It the event is now activated
	 */
	public boolean isTrackEvent()
	{
		return event;
	}

	private class EventFrame extends Frame
	{

		private boolean activateEvent;
		/**
		 * 	
		 * @param time Time of the frame
		 * @param event True means activating the event
		 */
		public EventFrame(Double time, boolean event) {
			 super(time);
			 activateEvent = event;
		}
		public boolean getActivateEvent() {
			return activateEvent;
		}
	}
}
